package com.sinog.biz.service.business.impl;

import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.service.business.SearchCodeListService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.core.domain.SystemCode;
import com.sinog.core.utilbean.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 行政区划实现类
 * @author -
 * @date -
 */
@Service
public class SearchCodeListServiceImpl implements SearchCodeListService {

    @Resource
    private SystemCodeMapper systemCodeMapper;

    /**
     * 犯罪类型code码查询
     * @param codeType codeType
     * @return List<Map < String,String>>
     */
    @Override
    public List<Map<String,String>> getCodeList(String codeType) {
        return systemCodeMapper.getSearchCodeResoult(codeType);
    }

    /**
     * 行政单位查询
     * @param codeType codeType
     * @param pcodeId pcodeId
     * @return List<Map < String,String>>
     */
    @Override
    public List<Map<String,String>> getCodeNativeList(String codeType,String pcodeId) {
        String localCode = CodeUrlUtils.getLocalCode();
        Map<String,Object> cloumMap = new HashMap<>(16);
        cloumMap.put("code_type",codeType);
        cloumMap.put("pcode_id",pcodeId);
        List<SystemCode> systemCodes = systemCodeMapper.selectByMap(cloumMap);
        List<Map<String,String>> list = new ArrayList<>(16);
        for(SystemCode sc : systemCodes) {
            Map<String,String> map = new HashMap<>(16);
            map.put("id",sc.getCodeId());
            map.put("text",sc.getCodeName());
            if(StringUtils.equals(Constants.NumberStr.NUM_STR_FU_ONE,pcodeId)) {
                // flag.equals("1") 代表只查询本省区划
                if(!StringUtils.equals(sc.getCodeId(),localCode)) {
                    list.add(map);
                } else if(StringUtils.equals(sc.getCodeId(),localCode)) {
                    list.add(0,map);
                }
            } else {
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 街道代码查询
     * @param pcodeId pcodeId
     * @return List<Map < String,String>>
     */
    @Override
    public List<Map<String,String>> getCodeJieDaoList(String pcodeId) {
        return systemCodeMapper.getSearchCodeJiedDaoResoult(pcodeId);
    }
}